package com.office.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.ai.embedding.EmbeddingClient;
import org.springframework.ai.ollama.OllamaEmbeddingClient;
import org.springframework.ai.ollama.api.OllamaApi;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.jdbc.core.JdbcTemplate;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Qualifier;

/**
 * ai配置
 * @author 数字牧马人
 */

@Configuration
@EnableAutoConfiguration(exclude = {
    org.springframework.ai.autoconfigure.vectorstore.pgvector.PgVectorStoreAutoConfiguration.class,
    org.springframework.ai.autoconfigure.ollama.OllamaAutoConfiguration.class,
    org.springframework.ai.autoconfigure.openai.OpenAiAutoConfiguration.class
})
public class AiConfig {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Bean("customOllamaEmbeddingClient")
    @Primary
    @ConditionalOnMissingBean
    public EmbeddingClient customOllamaEmbeddingClient() {
        return new OllamaEmbeddingClient(new OllamaApi());
    }

    @Bean("simpleVectorStore")
    @Primary
    @ConditionalOnMissingBean
    public VectorStore simpleVectorStore(@Qualifier("customOllamaEmbeddingClient") EmbeddingClient embeddingClient) {
        return new SimpleVectorStore(embeddingClient);
    }
}
